let fs = require('fs');
var express = require("express");
var app = express();
const bodyParser = require('body-parser');
const cors = require('cors');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: true
}));
app.use(cors());

//写入json文件选项 params 必须是对象
function changeJson(params) {
    fs.readFile('./person.json', function (err, data) {
        if (err) {
            console.error(err);
            //写入内容
            fs.writeFile('./person.json', '{"data":[],"total":0}', function (error) {
                if (error) {
                    console.log(error, "创建json文件失败  ");
                }
                changeJson(params)
            })
        } else {
            let person = data.toString();
            person = JSON.parse(person);
            //把数据读出来,然后进行修改
            let has = false
            for (let i = 0; i < person.data.length; i++) {
                if (params.id == person.data[i].id) {
                    has = true
                    console.log('id一样的');
                    for (let key in params) {
                        if (params[key]) {
                            person.data[i][key] = params[key];
                        }
                    }
                    break;
                }
            }
            // 如果查不到
            if (!has) {
                person.data.push(params)
            }
            person.total = person.data.length;
            let str = JSON.stringify(person);
            //console.log(str);
            fs.writeFile('./person.json', str, function (err) {
                if (err) {
                    console.error(err);
                }
                console.log('--------------------修改成功');
                console.log(person.data);
            })
        }

    })
}
//根据参数，查询数据
app.post('/getUserImg', function (req, res) {
    console.log(req.body)
    let params = req.body;
    let result = {
        code: 200,
        data: null,
        message: "查询用户img字符串成功"
    }
    try {
        fs.readFile('./person.json', function (err, data) {
            if (err) {
                console.error(err);
            }
            let person = data ? data.toString() : '{}';
            person = JSON.parse(person);
            //把数据读出来
            //console.log(person.data);
            let arr = person.data,
                obj = null;
            for (let i = 0; i < arr.length; i++) {
                if (arr[i].id == params.id) {
                    obj = arr[i]
                    break;
                }
            }
            result.data = obj
            res.json(result)
        })
    } catch (error) {
        result.code = 500;
        result.message = '服务器出错'
        result.error = error
        res.json(result)
    }
})
// 修改图片
app.post('/editUserImg',function (req, res) {
    let result = {
        code: 200,
        data: null,
        message: "修改成功"
    }
    try {
        changeJson(req.body)
        res.json(result)

    } catch (error) {
        result.code==500
        result.message = '服务器出错'
        result.error = error
        res.json(result)
        
    }
})

app.listen(3000, () => {
    console.log('http://127.0.0.1:3000');
})